﻿using System;
using System.IO;
using System.Windows.Media;
using System.Collections.Generic;

namespace Red.Core
{
    /// <summary>
    /// 
    /// </summary>
    public class ResourceMetadata
    {
        #region Data

        private static Dictionary<string, ResourceDirMetadata> s_cache = new Dictionary<string, ResourceDirMetadata>();
        const int dirsKeptInCache = 10;

        #endregion Data


        #region Properties

        /// <summary>
        /// 
        /// </summary>
        public string Path { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public ImageSource Thumbnail { get; set; }

        #endregion Properties

        #region Static methods

        /// <summary>
        /// 
        /// </summary>
        /// <param name="path">Full path to resource</param>
        /// <returns></returns>
        public static ResourceMetadata GetFor( string path )
        {
            var dir = System.IO.Path.GetDirectoryName( path );
            var file = System.IO.Path.GetFileName( path );
            return GetFor( dir, file );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dir"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        public static ResourceMetadata GetFor( string dir, string file )
        {
            var map = GetMapForDirectory( dir );
            ResourceMetadata data;
            map.TryGetValue( file, out data );
            return data;
        }

        #endregion Static methods

        #region Private methods

        static Dictionary<string, ResourceMetadata> GetMapForDirectory( string dir )
        {
            if ( s_cache.ContainsKey( dir ) )
            {
                return s_cache[ dir ].fileMap;
            }

            var dirData = new ResourceDirMetadata
            {
                timeStamp = DateTime.Now,
                fileMap = new Dictionary<string, ResourceMetadata>()
            };
            s_cache.Add( dir, dirData );

            try
            {
                var mtPath = System.IO.Path.Combine( dir, ".metadata" );

                if ( File.Exists( mtPath ) )
                {
                }
            }
            catch ( IOException )
            {
            }

            return dirData.fileMap;
        }

        static void CleanupCache()
        {
        }

        #endregion Private methods

        #region Private types

        private struct ResourceDirMetadata
        {
            public DateTime timeStamp;
            public Dictionary<string, ResourceMetadata> fileMap;
        }

        #endregion
    }
}